﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Moon.DBUtility;

namespace DAL
{
    	/// <summary>
    /// 数据访问类:SystemMoudelDAL
	/// </summary>
	public partial class SystemMoudelDAL
	{
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_systemmodule");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.SystemMoudelInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_systemmodule(");
            strSql.Append("ModulePage,ModuleName,@ICON)");
            strSql.Append(" values (");
            strSql.Append("@ModulePage,@ModuleName)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ModulePage", OleDbType.VarChar,50),
                    new OleDbParameter("@ICON", OleDbType.VarChar,50),
					new OleDbParameter("@ModuleName", OleDbType.VarChar,50),
                    new OleDbParameter("@ParentID",  OleDbType.Integer,4) };
            parameters[0].Value = model.ModulePage;
            parameters[1].Value = model.ICON;
            parameters[2].Value = model.ModuleName;
            parameters[3].Value = model.ModuleName;
            parameters[4].Value = model.ParentID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SystemMoudelInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_systemmodule set ");
            strSql.Append("ModulePage=@ModulePage,");
            strSql.Append("ModuleName=@ModuleName,");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("ICON=@ICON");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ModulePage", OleDbType.VarChar,50),
					new OleDbParameter("@ModuleName", OleDbType.VarChar,50),
                    new OleDbParameter("@ParentID", OleDbType.Integer,4),
                    new OleDbParameter("@ICON", OleDbType.VarChar,50),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = model.ModulePage;
            parameters[1].Value = model.ModuleName;
            parameters[2].Value = model.ParentID;
            parameters[3].Value = model.ICON;
            parameters[4].Value = model.ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_systemmodule ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_systemmodule ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SystemMoudelInfo GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ModulePage,ModuleName,ICON,ParentID from t_systemmodule ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            Model.SystemMoudelInfo model = new Model.SystemMoudelInfo();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ModulePage"] != null && ds.Tables[0].Rows[0]["ModulePage"].ToString() != "")
                {
                    model.ModulePage = ds.Tables[0].Rows[0]["ModulePage"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ModuleName"] != null && ds.Tables[0].Rows[0]["ModuleName"].ToString() != "")
                {
                    model.ModuleName = ds.Tables[0].Rows[0]["ModuleName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ICON"] != null && ds.Tables[0].Rows[0]["ICON"].ToString() != "")
                {
                    model.ICON = ds.Tables[0].Rows[0]["ICON"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ParentID"] != null && ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID =  int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ModulePage,ModuleName,ICON,ParentID ");
            strSql.Append(" FROM t_systemmodule ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_systemmodule ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string[] orderby, int startIndex, int endIndex)
        {
            //獲得查詢條數
            int count = endIndex - startIndex;
            //查詢條件
            string where = "";
            //內排序條件
            string Inorderby = "order by ID desc";
            //外排序條件
            string Outorderby = "order by ID ASC";
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                where += " where 1=1 " + strWhere;
                if (orderby.Length > 1)
                {
                    //0號位置存取排序字段
                    Inorderby = "order by " + orderby[0].Trim() + " " + orderby[1].Trim();
                    //1號位置為排序字段
                    if (orderby[1].Trim().ToLower().Equals("desc"))
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " asc";
                    }
                    else
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " desc";
                    }
                }
            }
            else
            {
                if (orderby.Length > 1)
                {
                    //0號位置存取排序字段
                    Inorderby = "order by " + orderby[0].Trim() + " " + orderby[1].Trim();
                    //1號位置為排序字段
                    if (orderby[1].Trim().ToLower().Equals("desc"))
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " asc";
                    }
                    else
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " desc";
                    }
                }
            }

            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select * from (select top {0} * from (select top {1} * from t_systemmodule {2} {3} ) {4}) {3}", count, endIndex, where, Inorderby, Outorderby);

            return DbHelperOleDb.Query(strSql.ToString());
        }
        #endregion  Method
    }
}
